package com.lrkj.web.controller.system;

import com.google.gson.JsonObject;
import com.lrkj.common.core.domain.AjaxResult;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.payfee.service.IReceiptService;
import com.lrkj.sys.vo.HomePageVo;
import com.lrkj.weixin.entity.WxOrderRecord;
import com.lrkj.weixin.service.impl.IWxPaymentService;
import com.lrkj.workorder.domain.WorkOrder;
import com.lrkj.workorder.domain.WorkOrderType;
import com.lrkj.workorder.service.IWorkOrderService;
import com.lrkj.workorder.service.IWorkOrderTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author huxy
 * @version V1.0
 * @创建时间
 * @备注
 * @类名
 * @节点
 */
@RequestMapping("/home/index/")
@RestController
public class HomePageController {
    @Autowired
    private IWxPaymentService wxPaymentService;

    @Autowired
    private IWorkOrderService workOrderService;

    @Autowired
    private IWorkOrderTypeService workOrderTypeService;

    @Autowired
    private IReceiptService receiptService;

    @GetMapping("init")
    public AjaxResult init() {
        String time = DateUtils.dateTimeNow("yyyy-MM-dd");
        List<HomePageVo> listBack = new ArrayList<>();
        final int[] ids = {1, 2, 3, 4};
        final String[] icons = {"JRSK", "JRCZ", "", "TSJY", "BSBX","",""};
        //今日收款
        HomePageVo homePageVo = new HomePageVo();
        BigDecimal receiptCount = receiptService.indexShow(time);
        homePageVo.setCount(receiptCount == null ? BigDecimal.ZERO : receiptCount);
        homePageVo.setId(ids[0]);
        homePageVo.setIcon(icons[0]);
        homePageVo.setText("今日收款");
        listBack.add(homePageVo);

        List<WxOrderRecord> list = wxPaymentService.indexShow(time);
        HomePageVo homePageVoRecord = new HomePageVo();
        homePageVoRecord.setId(2);
        homePageVoRecord.setIcon(icons[1]);
        homePageVoRecord.setText("今日充值");
        if (list.size() == 0) {
            homePageVoRecord.setCount(BigDecimal.ZERO);
        } else {
            Map<Integer, BigDecimal> map = list.stream().collect(Collectors.toMap(WxOrderRecord::getOrderState, WxOrderRecord::getOrderPrice));
            homePageVoRecord.setCount(map.containsKey(1) ? map.get(1) : BigDecimal.ZERO);
            homePageVoRecord.setBadCount(map.containsKey(0) ? map.get(0) : BigDecimal.ZERO);
        }
        listBack.add(homePageVoRecord);
        List<WorkOrder> alist = workOrderService.indexShow(null);
        listBack.addAll(alist.stream().map(a -> {
            HomePageVo WorkOrderRecord = new HomePageVo();
            WorkOrderRecord.setCount(new BigDecimal(a.getCounts()));
            WorkOrderRecord.setText(getWorkOrderTypeName(a.getId()));
            WorkOrderRecord.setId(ids[2]);
            WorkOrderRecord.setIcon(icons[ids[2]]);
            WorkOrderRecord.setAuxiliaryId(a.getId());
            ids[2]++;
            return WorkOrderRecord;
        }).collect(Collectors.toList()));
        return AjaxResult.success(listBack);
    }

    public String getWorkOrderTypeName(String id) {
        WorkOrderType workOrderType = workOrderTypeService.selectWorkOrderTypeById(id);
        if (workOrderType != null) {
            return workOrderType.getTypeName();
        }
        return "";
    }
}
